#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>

int main()
{
  pid_t id = fork();
  if(id < 0)
    exit(1);
  else if (id == 0)
  {
    // child process
    int k = 6;
    printf("i am child process\n");
    while(--k)
    {
      sleep(1);
      printf("%d ",k);
      fflush(stdout);
    }
    // printf("\n");
  }
  else 
  {
    int status = 0;
    pid_t ret = waitpid(-1, &status, 0);
    if(ret > 0)
    {
      if(WIFEXITED(status))
      {
        // 等成功
        printf("waitting success,exit code:%d ,core dump code:%d\n",WEXITSTATUS(status), WIFEXITED(status) >> 7 & 0x1);
      }
      else 
      {
        printf("waitting fail, core dump code:%d\n", WIFEXITED(status));
      }
    }
  }
}
